package controllers;

import models.Lecture;
import models.Question;
import models.User;
import models.Vote;
import models.Answer;
import play.mvc.*;

public class Votes extends Controller {

    public static void lectureUpVote(Long lectureId) {
    	User user = Security.getConnected();
    	Lecture lecture = Lecture.findById(lectureId);
    	Vote vote = Vote.getVote(user, lecture);
    	if (vote == null)
    		new Vote(user, lecture, true);
    	else {
    		vote.positive = true;
    		vote.save();
    	}
    	Lectures.show(lectureId);
    }
    
    public static void lectureDownVote(Long lectureId) {
    	User user = Security.getConnected();
    	Lecture lecture = Lecture.findById(lectureId);
    	Vote vote = Vote.getVote(user, lecture);
    	if (vote == null)
    		new Vote(user, lecture, false);
    	else {
    		vote.positive = false;
    		vote.save();
    	}
    	Lectures.show(lectureId);
    }
    
    public static void answerUpVote(Long answerId) {
    	User user = Security.getConnected();
    	Answer answer = Answer.findById(answerId);
    	Vote vote = Vote.getVote(user, answer);
    	Question question = answer.question;
    	if (vote == null)
    		new Vote(user, answer, true);
    	else {
    		vote.positive = true;
    		vote.save();
    	}
    	Answers.show(question.id);
    }
    
    public static void answerDownVote(Long answerId) {
    	User user = Security.getConnected();
    	Answer answer = Answer.findById(answerId);
    	Vote vote = Vote.getVote(user, answer);
    	Question question = answer.question;
    	if (vote == null)
    		new Vote(user, answer, false);
    	else {
    		vote.positive = false;
    		vote.save();
    	}
    	Answers.show(question.id);
    }

}
